#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <unistd.h>

int client_running = 1;

void sigint_hanler(int sig) 
{
    client_running = 0;
    printf("client is to exit.");
}

int main()
{
    int fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        perror("socket creation error");
        return -1;
    }

    struct sockaddr_in addr;
    memset(&addr, '\0', sizeof addr);
    
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr("127.0.0.1");
    addr.sin_port = htons(8080);

    int n = connect(fd, (struct sockaddr*)&addr, sizeof(addr));
    if (n < 0) {
        perror("connect error");
        return -2;
    }

    while (client_running) {
        char buffer[128] = {0};
        fgets(buffer, 128, stdin);

        send(fd, buffer, strlen(buffer), 0);
    }

    close(fd);

    return 0;
}